﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0
 *  ----------------------------------------------------------------------------
 *  File:       PhysicsSprite.cs
 *  Author:     Shane-PC\Shane
 *  ----------------------------------------------------------------------------
 */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames.FarseerPhysics.Collisions;

namespace EuphoricEngine.Components.Components2D
{
    public class PhysicsSprite : Sprite
    {
        private Body _body;
        private Geom _geom;

        public PhysicsSprite(Vector2 position, Body body, Geom geom)
            : base(position)
        {
            _body = body;
            _geom = geom;

            _body.Position = position;
        }
        public PhysicsSprite(string texturePath, Vector2 position, Body body, Geom geom)
            : base(texturePath, position)
        {
            _body = body;
            _geom = geom;

            _body.Position = position;
        }
        public PhysicsSprite(Texture2D texture, Vector2 position, Body body, Geom geom)
            : base(texture, position)
        {
            _body = body;
            _geom = geom;

            _body.Position = position;
        }

        public override void Update()
        {
            SetPosition(_body.Position);
            SetRotation(_body.Rotation);

            if (_body != null && _geom != null)
            {
                if (_geom.Body == null)
                    _geom.SetBody(_body);
            }

            base.Update();
        }

        public void SetBody(Body body)
        {
            _body = body;

            if (_geom != null)
                _geom.SetBody(_body);
        }

        public void SetGeom(Geom geom)
        {
            _geom = geom;

            if (_geom.Body != null && _body != null)
                _geom.SetBody(_body);
        }

        public void SetCollisionCategories(CollisionCategory collisionCategories)
        {
            _geom.CollisionCategories = collisionCategories;
        }
        public void SetCollidesWith(CollisionCategory collidesWith)
        {
            _geom.CollidesWith = collidesWith;
        }

        public override void SetScale(float scale)
        {
            throw new NotImplementedException();
        }

        public override void SetRotation(float rotation)
        {
            _body.Rotation = rotation;
            base.SetRotation(_body.Rotation);
        }

        public override void SetPosition(Vector2 position)
        {
            _body.Position = position;
            base.SetPosition(_body.Position);
        }

        public override void SetPositionX(float X)
        {
            SetPosition(new Vector2(X, _body.Position.Y));
        }

        public override void SetPositionY(float Y)
        {
            SetPosition(new Vector2(_body.Position.X, Y));
        }

        public bool IsStatic
        {
            get { return _body.IsStatic; }
            set { _body.IsStatic = value; }
        }
        public bool IgnoreGravity
        {
            get { return _body.IgnoreGravity; }
            set { _body.IgnoreGravity = value; }
        }
        public Body Body { get { return _body; } set { if (value != null) { _body = value; } } }
        public Geom Geom { get { return _geom; } set { if (value != null) { _geom = value; } } }
    }
}
